package com.qianfeng.propertygroup.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.propertygroup.constants.ResultCode;
import com.qianfeng.propertygroup.dao.UserDao;
import com.qianfeng.propertygroup.exceptions.DeleteErrorException;
import com.qianfeng.propertygroup.exceptions.GetErrorException;
import com.qianfeng.propertygroup.pojo.User;
import com.qianfeng.propertygroup.service.intf.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/***
 *
 *   █████▒█    ██  ▄████▄   ██ ▄█▀       ██████╗ ██╗   ██╗ ██████╗
 * ▓██   ▒ ██  ▓██▒▒██▀ ▀█   ██▄█▒        ██╔══██╗██║   ██║██╔════╝
 * ▒████ ░▓██  ▒██░▒▓█    ▄ ▓███▄░        ██████╔╝██║   ██║██║  ███╗
 * ░▓█▒  ░▓▓█  ░██░▒▓▓▄ ▄██▒▓██ █▄        ██╔══██╗██║   ██║██║   ██║
 * ░▒█░   ▒▒█████▓ ▒ ▓███▀ ░▒██▒ █▄       ██████╔╝╚██████╔╝╚██████╔╝
 *  ▒ ░   ░▒▓▒ ▒ ▒ ░ ░▒ ▒  ░▒ ▒▒ ▓▒       ╚═════╝  ╚═════╝  ╚═════╝
 *  ░     ░░▒░ ░ ░   ░  ▒   ░ ░▒ ▒░
 *  ░ ░    ░░░ ░ ░ ░        ░ ░░ ░
 *           ░     ░ ░      ░  
 * Created with IntelliJ IDEA.
 * User: DELL
 * Date: 2020/12/21
 * Time: 20:49
 * @author 李强
 * 实现类
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public PageInfo<User> getAllUsers(int pageNum, int pageSize, String username, String dstatus) {
        PageHelper.startPage(pageNum, pageSize);
        try {
            List<User> allUsers = userDao.getAllUsers(username, dstatus);

            allUsers.forEach(user -> {
                user.setPassword(null);
            });
            return new PageInfo<>(allUsers);
        } catch (Exception e) {
            e.printStackTrace();
        }
        throw new GetErrorException("查询异常", ResultCode.ERROR);
    }

    @Override
    public void deleteUserByIds(List<String> ids) {
        if (ids == null || ids.size() == 0) {
            throw new DeleteErrorException("没有传递 id", ResultCode.NULL_DATA);
        }

        try {
            userDao.deleteUserByIds(ids);
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }
        throw new DeleteErrorException("删除失败", ResultCode.NULL_DATA);

    }
}
